﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MTGProject;
using System.IO;

namespace CardEditor
{
    public enum CardEditorMode
    {
        CREATURE,
        SORCERY
    }

    public partial class Form1 : Form
    {
        CardEditorMode activeMode = CardEditorMode.CREATURE;
        ICard activeCard = null;
        private OpenFileDialog fileSelectionDialog;
        string cardEdition = "unknown";
        string cardNumber = "unknown";

        public Form1()
        {
            InitializeComponent();

            if(File.Exists("resources\\deckeditor_background.png"))
                this.BackgroundImage = new System.Drawing.Bitmap("resources\\deckeditor_background.png");

            //open dialog box
            fileSelectionDialog = new OpenFileDialog();
            fileSelectionDialog.Filter = "MTGProject Card (*.xml)|*.xml|All files (*.*)|*.*";
            fileSelectionDialog.Title = "Select a card to load:";

            for (int x = 0; x < Settings.CARD_CREATURE_KEYWORDS; x++)
                checkedListKeywords.Items.Add(((CreatureKeyword)x));

            for (int x = 0; x < Settings.CARD_TYPE_COUNT; x++)
                checkedListTypes.Items.Add(((CreatureType)x));

            for (int x = 0; x < 6; x++)
                checkedListColors.Items.Add(((ManaColor)x));

            //set default card image
            pictureBoxCard.Image = new System.Drawing.Bitmap("images\\generic_cardback.jpg");

            UpdateCardControlAvailability();
        }

        #region GUI events

        private void buttonNew_Click(object sender, EventArgs e)
        {
            cardEdition = "unknown";
            cardNumber = "unknown";

            textBoxName.Text = "";
            textBoxPower.Text = "0";
            textBoxToughness.Text = "0";
            textBoxScript.Text = "";

            //reset checked items
            for (int x = 0; x < checkedListKeywords.Items.Count; x++)
                checkedListKeywords.SetItemChecked((int)x, false);

            for (int x = 0; x < checkedListTypes.Items.Count; x++)
                checkedListTypes.SetItemChecked((int)x, false);

            for (int x = 0; x < checkedListColors.Items.Count; x++)
                checkedListColors.SetItemChecked((int)x, false);

            textBoxWhite.Text = "0";
            textBoxBlue.Text = "0";
            textBoxBlack.Text = "0";
            textBoxRed.Text = "0";
            textBoxGreen.Text = "0";
            textBoxColorless.Text = "0";
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            if (fileSelectionDialog.ShowDialog() == DialogResult.OK)
            {
                string filenameFullPath = fileSelectionDialog.FileName;
                string actualFilename = filenameFullPath.Substring(filenameFullPath.IndexOf("cards\\") + 6);
                string filenameImage = filenameFullPath;

                cardEdition = actualFilename.Split('-')[0];
                cardNumber = actualFilename.Split('-')[1];

                //load card object
                ICard card = CardIO.LoadXMLCard(filenameFullPath, false);

                //load card image
                filenameImage = "images\\" + actualFilename.Substring(0, actualFilename.Length - 4) + ".jpg";

                if (File.Exists(filenameImage))
                    pictureBoxCard.Image = new System.Drawing.Bitmap(filenameImage);
                else
                    pictureBoxCard.Image = new System.Drawing.Bitmap("images\\generic_cardback.jpg");

                if (card is CardCreature)
                {
                    activeMode = CardEditorMode.CREATURE;
                    activeCard = (CardCreature)card;
                    UpdateGUIFromCard();
                    UpdateCardControlAvailability();
                }
                else if (card is CardSorcery)
                {
                    activeMode = CardEditorMode.SORCERY;
                    activeCard = (CardSorcery)card;
                    UpdateGUIFromCard();
                    UpdateCardControlAvailability();
                }
                else
                {
                    MessageBox.Show("Currently, only creatures and sorceries are editable.");
                }
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            UpdateCardFromGUI();

            string filename = cardEdition + "-" + cardNumber + "-" + activeCard.Name + ".xml";
            filename = filename.ToLower();
            filename = filename.Replace(" ", "");

            filename = CardIO.cardPath + filename;

            if (File.Exists(filename))
                File.Delete(filename);

            CardIO.WriteXMLCard(activeCard, filename);
        }

        #endregion

        #region methods

        private void UpdateCardControlAvailability()
        {
            //disable all editing controls
            textBoxName.Enabled = false;
            textBoxScript.Enabled = false;
            checkedListKeywords.Enabled = false;
            checkedListTypes.Enabled = false;
            checkedListColors.Enabled = false;
            textBoxPower.Enabled = false;
            textBoxToughness.Enabled = false;
            textBoxWhite.Enabled = false;
            textBoxBlue.Enabled = false;
            textBoxBlack.Enabled = false;
            textBoxRed.Enabled = false;
            textBoxGreen.Enabled = false;
            textBoxColorless.Enabled = false;
            textBoxImageURL.Enabled = false;

            if (activeMode == CardEditorMode.CREATURE)
            {
                textBoxName.Enabled = true;
                textBoxScript.Enabled = true;
                checkedListKeywords.Enabled = true;
                checkedListTypes.Enabled = true;
                checkedListColors.Enabled = true;
                textBoxPower.Enabled = true;
                textBoxToughness.Enabled = true;
                textBoxWhite.Enabled = true;
                textBoxBlue.Enabled = true;
                textBoxBlack.Enabled = true;
                textBoxRed.Enabled = true;
                textBoxGreen.Enabled = true;
                textBoxColorless.Enabled = true;
                textBoxImageURL.Enabled = true;
            }
            else if (activeMode == CardEditorMode.SORCERY)
            {
                textBoxName.Enabled = true;
                textBoxScript.Enabled = true;
                checkedListColors.Enabled = true;
                textBoxWhite.Enabled = true;
                textBoxBlue.Enabled = true;
                textBoxBlack.Enabled = true;
                textBoxRed.Enabled = true;
                textBoxGreen.Enabled = true;
                textBoxColorless.Enabled = true;
                textBoxImageURL.Enabled = true;
            }
            else
                throw new Exception("Encounted unknown card type in UpdateCardControlAvailability().");

        }

        public void UpdateCardFromGUI()
        {
            string name = textBoxName.Text;
            LinkedList<ManaColor> colors = new LinkedList<ManaColor>();
            LinkedList<ManaColor> cost = new LinkedList<ManaColor>();

            //colors
            foreach (object obj in checkedListColors.CheckedItems)
            {
                for (int x = 0; x < 6; x++)//HACK: just because
                    if (((ManaColor)x).ToString().Equals(obj.ToString()))
                        colors.AddLast(((ManaColor)x));
            }

            //cost
            for(int x = 0; x < int.Parse(textBoxWhite.Text); x++)
                cost.AddLast(ManaColor.WHITE);
            for (int x = 0; x < int.Parse(textBoxBlue.Text); x++)
                cost.AddLast(ManaColor.BLUE);
            for (int x = 0; x < int.Parse(textBoxBlack.Text); x++)
                cost.AddLast(ManaColor.BLACK);
            for (int x = 0; x < int.Parse(textBoxRed.Text); x++)
                cost.AddLast(ManaColor.RED);
            for (int x = 0; x < int.Parse(textBoxGreen.Text); x++)
                cost.AddLast(ManaColor.GREEN);
            for (int x = 0; x < int.Parse(textBoxColorless.Text); x++)
                cost.AddLast(ManaColor.NONE);

            //script
            string script = textBoxScript.Text;

            //image url
            string imageURL = textBoxImageURL.Text;

            if (activeMode == CardEditorMode.CREATURE)
            {
                LinkedList<CreatureType> types = new LinkedList<CreatureType>();
                LinkedList<CreatureKeyword> keywords = new LinkedList<CreatureKeyword>();
                int power = int.Parse(textBoxPower.Text);
                int toughness = int.Parse(textBoxToughness.Text);

                //types
                foreach (object obj in checkedListTypes.CheckedItems)
                {
                    for (int x = 0; x < Settings.CARD_TYPE_COUNT; x++)
                        if (((CreatureType)x).ToString().Equals(obj.ToString()))
                            types.AddLast(((CreatureType)x));
                }

                //keywords
                foreach (object obj in checkedListKeywords.CheckedItems)
                {
                    for (int x = 0; x < Settings.CARD_CREATURE_KEYWORDS; x++)
                        if (((CreatureKeyword)x).ToString().Equals(obj.ToString()))
                            keywords.AddLast(((CreatureKeyword)x));
                }

                activeCard = new CardCreature(name, colors, cost, types, keywords, power, toughness, script, imageURL, null);
            }
            else if (activeMode == CardEditorMode.SORCERY)
            {
                activeCard = new CardSorcery(name, colors, cost, script, imageURL, null);
            }
            else
                throw new Exception("Form1.UpdateCardFromGUI(): Encounted unknown editor mode.");
        }

        public void UpdateGUIFromCard()
        {
            if (activeCard != null)
            {
                buttonNew_Click(null, null);

                //update name
                textBoxName.Text = activeCard.Name;

                //load checked lists
                foreach (ManaColor color in activeCard.Colors)
                {
                    checkedListColors.SetItemChecked((int)color, true);
                }

                //cost
                foreach (ManaColor color in activeCard.Cost)
                    if (color == ManaColor.WHITE)
                        textBoxWhite.Text = (int.Parse(textBoxWhite.Text) + 1).ToString();
                foreach (ManaColor color in activeCard.Cost)
                    if (color == ManaColor.BLUE)
                        textBoxBlue.Text = (int.Parse(textBoxBlue.Text) + 1).ToString();
                foreach (ManaColor color in activeCard.Cost)
                    if (color == ManaColor.BLACK)
                        textBoxBlack.Text = (int.Parse(textBoxBlack.Text) + 1).ToString();
                foreach (ManaColor color in activeCard.Cost)
                    if (color == ManaColor.RED)
                        textBoxRed.Text = (int.Parse(textBoxRed.Text) + 1).ToString();
                foreach (ManaColor color in activeCard.Cost)
                    if (color == ManaColor.GREEN)
                        textBoxGreen.Text = (int.Parse(textBoxGreen.Text) + 1).ToString();
                foreach (ManaColor color in activeCard.Cost)
                    if (color == ManaColor.NONE)
                        textBoxColorless.Text = (int.Parse(textBoxColorless.Text) + 1).ToString();

                //script
                textBoxScript.Text = activeCard.Script;

                //image url
                textBoxImageURL.Text = activeCard.ImageURL;

                if (activeCard is CardCreature)
                {
                    //load checked lists
                    foreach (CreatureKeyword keyword in ((CardCreature)activeCard).Keywords)
                    {
                        checkedListKeywords.SetItemChecked((int)keyword, true);
                    }
                    foreach (CreatureType type in ((CardCreature)activeCard).Types)
                    {
                        checkedListTypes.SetItemChecked((int)type, true);
                    }

                    //update power and toughness
                    textBoxPower.Text = ((CardCreature)activeCard).Power.ToString();
                    textBoxToughness.Text = ((CardCreature)activeCard).Toughness.ToString();
                }
                else if (activeCard is CardSorcery)
                {
                }
                else
                    throw new Exception("Encounted unknown card type in UpdateGUIFromCard()");
            }
        }

        #endregion
    }
}
